<script setup>
defineOptions({ name: 'LoginPage' })
import { User, Lock } from '@element-plus/icons-vue'
import { ref } from 'vue'
import { loginService, registerService } from '@/api/login'
import { useUserStore } from '@/stores'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import 'element-plus/dist/index.css'
// 是否登录
const isRegister = ref(false)
// 表单数据
const userForm = ref({
  username: '',
  password: '',
  repassword: ''
})
// 表单规则
const validateRepassword = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('再次密码不能为空'))
  } else if (value !== userForm.value.password) {
    callback(new Error('两次密码输入不一致!'))
  } else {
    callback()
  }
}
const userRules = {
  username: [
    { required: true, message: '用户名不能为空', trigger: 'blur' },
    {
      pattern: /^[a-zA-Z0-9]{1,10}$/,
      message: '1-10位大小写字母和数字',
      trigger: 'blur'
    }
  ],
  password: [
    { required: true, message: '密码不能为空', trigger: 'blur' },
    {
      pattern: /^\S{6,15}$/,
      message: '1-10位大小写字母和数字',
      trigger: 'blur'
    }
  ],
  repassword: [{ validator: validateRepassword, trigger: 'blur' }]
}
// 执行登录
const router = useRouter()
const refForm = ref(null)
const login = async () => {
  if (!refForm.value) return
  refForm.value.validate(async (valid) => {
    if (!valid) {
      return
    }
    const res = await loginService(userForm.value)
    useUserStore().setToken(res.token)
    ElMessage.success(res.message)
    router.push('/')
  })
}
// 执行注册
const regiter = async () => {
  if (!refForm.value) return
  refForm.value.validate(async (valid) => {
    if (!valid) {
      return
    }
    const res = await registerService(userForm.value)
    ElMessage.success(res.message)
    isRegister.value = false
  })
}
</script>
<template>
  <el-row class="login-page">
    <el-col :span="12"></el-col>
    <el-col :span="6" :offset="3">
      <el-form
        v-if="isRegister === false"
        ref="refForm"
        size="large"
        :model="userForm"
        :rules="userRules"
      >
        <el-form-item class="item1">登录</el-form-item>
        <el-form-item prop="username">
          <el-input
            placeholder="请输入用户名"
            :prefix-icon="User"
            v-model="userForm.username"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            placeholder="请输入密码"
            :prefix-icon="Lock"
            v-model="userForm.password"
          ></el-input>
        </el-form-item>
        <el-form-item class="item4">
          <el-checkbox>记住我</el-checkbox>
          <el-link href="#" :underline="false" type="primary"
            >忘记密码？</el-link
          >
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="login()">登录</el-button>
        </el-form-item>
        <el-form-item class="flex">
          <el-link type="info" :underline="false" @click="isRegister = true">
            → 注册
          </el-link>
        </el-form-item>
      </el-form>
      <el-form
        v-else
        ref="refForm"
        size="large"
        :model="userForm"
        :rules="userRules"
      >
        <el-form-item class="item1">注册</el-form-item>
        <el-form-item prop="username">
          <el-input
            placeholder="请输入用户名"
            :prefix-icon="User"
            v-model="userForm.username"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            placeholder="请输入密码"
            :prefix-icon="Lock"
            v-model="userForm.password"
          ></el-input>
        </el-form-item>
        <el-form-item prop="repassword">
          <el-input
            placeholder="请输入确认密码"
            :prefix-icon="Lock"
            v-model="userForm.repassword"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="regiter()">注册</el-button>
        </el-form-item>
        <el-form-item class="flex">
          <el-link type="info" :underline="false" @click="isRegister = false">
            ← 返回
          </el-link>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>
<style lang="scss" scoped>
.login-page {
  height: 100vh;
  background-color: #fff;
  .el-col:nth-child(1) {
    background:
      url('@/assets/logo2.png') no-repeat 60% center / 280px auto,
      url('@/assets/login_bg.jpg') no-repeat center / cover;
  }
  :deep(.el-col:nth-child(2)) {
    // background-color: aquamarine;
    display: flex;
    align-items: center;
    justify-content: center;
    .el-form {
      height: 50%;
      width: 100%;
      // background-color: azure;
      .item1 .el-form-item__content {
        font-size: 80px;
        font-weight: 500;
      }
      .item4 .el-form-item__content {
        justify-content: space-between;
        // font-size: 12px;
        // .el-checkbox .el-checkbox__label,
        // .el-link .el-link__inner {
        //   font-size: 25px;
        // }
        // .el-checkbox .el-checkbox__input .el-checkbox__inner {
        //   width: 25px;
        //   height: 25px;
        // }
      }
      .el-button {
        width: 100%;
      }
    }
  }
}
</style>
